package com.example.apidemo2;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

public class BaseActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		ViewGroup viewGroup = getContentView();
		int[] screenSize = getScreenSize(this);
		if (screenSize[0] > screenSize[1]) {
			FrameLayout.LayoutParams lp = (LayoutParams) viewGroup.getLayoutParams();
			lp.width = screenSize[0] / 2;
			lp.gravity = Gravity.CENTER;
			viewGroup.setLayoutParams(lp);
		}
	}
	
	private ViewGroup getContentView(){
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        ViewGroup content = (ViewGroup) view.getChildAt(0);
        return content;
    }
	
	public  int[] getScreenSize(Context activity) {
		// 获取屏幕密度（方法2）
		DisplayMetrics dm = new DisplayMetrics();
//		Log.d(TAG, "activity" + activity);
		dm = activity.getResources().getDisplayMetrics();
		float density = dm.density; // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
		int densityDPI = dm.densityDpi; // 屏幕密度（每寸像素：120/160/240/320）
		float xdpi = dm.xdpi;
		float ydpi = dm.ydpi;
		int screenWidth = dm.widthPixels; // 屏幕宽（像素，如：480px）
		int screenHeight = dm.heightPixels; // 屏幕高（像素，如：800px）
		int[] screens = { screenWidth, screenHeight };
		return screens;
	}

}
